package com.lijianan.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lijianan.entity.User;
import com.lijianan.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    @Autowired
    private UserMapper usersMapper;

    @Override
    public boolean register(String account, String password, String name, String company_name, String phone_number) {
        if (selectByAccount(account) != null) {
            System.out.println("该账号已经被注册！");
            return false;
        }
        User user = new User();
        user.setAccount(account);
        user.setPassword(encoder.encode(password));
        user.setRole("customer");
        user.setName(name);
        user.setCompany_name(company_name);
        user.setPhone_number(phone_number);
        return usersMapper.insert(user) != 0;
    }

    @Override
    public boolean login(String account, String password) {
        User user = selectByAccount(account);
        if (user == null) {
            System.out.println("该账号不存在！");
            return false;
        }
        if (!user.getIs_approved()) {
            System.out.println("该账号还未审核通过！");
            return false;
        }
        if (user.getIs_frozen()) {
            System.out.println("该账号已冻结！");
            return false;
        }
        if (!encoder.matches(password, user.getPassword())) {
            System.out.println("密码错误！");
            return false;
        }
        return true;
    }

    @Override
    public User selectByAccount(String account) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        return usersMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean modifyInfo(String account, String name, String company_name, String phone_number) {
        User user = selectByAccount(account);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("account", account).set("name", name).set("company_name", company_name).set("phone_number", phone_number);
        return usersMapper.update(user, updateWrapper) != 0;
    }

    @Override
    public boolean modifyRole(String account, String role) {
        User user = selectByAccount(account);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("account", account).set("role", role);
        return usersMapper.update(user, updateWrapper) != 0;
    }

    @Override
    public User selectByUserId(Integer userId) {
        return usersMapper.selectById(userId);
    }

    @Override
    public boolean changePassword(String account, String oldPassword, String newPassword) {
        User user = selectByAccount(account);
        if (!encoder.matches(oldPassword, user.getPassword())) {
            System.out.println("旧密码输入错误！");
            return false;
        } else {
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("account", account).set("password", encoder.encode(newPassword));
            return usersMapper.update(user, updateWrapper) != 0;
        }
    }

    @Override
    public boolean changeStateToApprove(Integer user_id) {
        User user = selectByUserId(user_id);
        if (user == null) {
            System.out.println("该用户不存在！");
            return false;
        }
        user.setIs_approved(true);
        return usersMapper.updateById(user) != 0;
    }

    @Override
    public boolean removeApproveState(Integer user_id) {
        User user = selectByUserId(user_id);
        if (user == null) {
            System.out.println("该用户不存在！");
            return false;
        }
        user.setIs_approved(false);
        return usersMapper.updateById(user) != 0;
    }

    @Override
    public boolean changeStateToFrozen(Integer user_id) {
        User user = selectByUserId(user_id);
        if (user == null) {
            System.out.println("该用户不存在！");
            return false;
        }
        user.setIs_frozen(true);
        return usersMapper.updateById(user) != 0;
    }

    @Override
    public boolean removeFrozenState(Integer user_id) {
        User user = selectByUserId(user_id);
        if (user == null) {
            System.out.println("该用户不存在！");
            return false;
        }
        user.setIs_frozen(false);
        return usersMapper.updateById(user) != 0;
    }

    @Override
    public boolean logoff(Integer user_id) {
        return usersMapper.deleteById(user_id) != 0;
    }

    @Override
    public List<User> selectAll() {
        return usersMapper.selectList(null);
    }
}
